package io.sage.service.impl;

import java.io.File;
import java.io.InputStream;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;

import io.sage.service.QnUploadService;

@Service
public class QnUploadServiceImpl implements QnUploadService, InitializingBean{
	
	@Autowired
    private UploadManager uploadManager;

    @Autowired
    private BucketManager bucketManager;

    @Autowired
    private Auth auth;

    @Value("${qiniu.bucket}")
    private String bucket;
    
    @Value("${qiniu.cdnPrefix}")
    private String cdnPrefix;

    /**
     * 定义七牛云上传的相关策略
     */
    private StringMap putPolicy;
	
    /**
     * 以文件的形式上传
     * @param file
     * @return
     * @throws QiniuException
     */
	@Override
	public String uploadFile(File file, String fileName) throws QiniuException {
		 Response response = this.uploadManager.put(file, fileName, getUploadToken());
	        int retry = 0;
	        while (response.needRetry() && retry < 3) {
	            response = this.uploadManager.put(file, fileName, getUploadToken());
	            retry++;
	        }
	        if (response.statusCode == 200) {
//	            return new StringBuffer().append(cdnPrefix).append(fileName).toString();
	        	return new StringBuffer().append(fileName).toString();//不添加返回域名
	        }
	        
	        return "上传失败!";
	}
	
	/**
     * 以流的形式上传
     *
     * @param inputStream
     * @return
     * @throws QiniuException
     */
	@Override
	public String uploadFile(InputStream inputStream, String fileName) throws QiniuException {
		 Response response = this.uploadManager.put(inputStream, fileName, getUploadToken(), null, null);
	        int retry = 0;
	        while (response.needRetry() && retry < 3) {
	            response = this.uploadManager.put(inputStream, fileName, getUploadToken(), null, null);
	            retry++;
	        }
	        if (response.statusCode == 200) {
//	            return new StringBuffer().append(cdnPrefix).append(fileName).toString();
	        	return new StringBuffer().append(fileName).toString();//不添加返回域名
	        }
	        return "上传失败!";
	}
	
	/**
     * 删除七牛云上的相关文件
     * @param key
     * @return
     * @throws QiniuException
     */
	@Override
	public String delete(String key) throws QiniuException {
		
		Response response = bucketManager.delete(this.bucket, key);
        int retry = 0;
        while (response.needRetry() && retry++ < 3) {
            response = bucketManager.delete(bucket, key);
        }
        return response.statusCode == 200 ? "删除成功!" : "删除失败!";
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		
		this.putPolicy = new StringMap();
        putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}");
		
	}
	
	/**
     * 获取上传凭证
     * @return
     */
	private String getUploadToken() {

		return this.auth.uploadToken(bucket, null, 3600, putPolicy);
	}

}
